﻿if (typeof Axis == "undefined") Axis = {};
if (typeof Axis.View == "undefined") Axis.View = {};
if (typeof Axis.View.Member == "undefined") Axis.View.Member = {};
Axis.View.Member.Login = {
    Init: function () {
        var thisObj = this;
        thisObj.Event();
    },
    Toggle: function () {
        var thisObj = this;
        if ($("#Form-Login").is(":visible")) {
            thisObj.Hide();
        }
        else {
            thisObj.Clear();
            thisObj.Show();
        }
    },
    Show: function () {
        $("#Form-Login").slideDown("slow", function () {
            $("#txt_LoginUserName").focus();
        });

    },
    Hide: function () {
        $("#Form-Login").slideUp();
    },
    Clear: function () {
        $("#Form-Login").find("input").val("");
        $("#Form-Login").find("#lbl_LoginMessages").text("").hide();
    },
    Submit: function () {
        var thisObj = this;
        var pData = {
            pEmail: $("#txt_LoginUserName").val() || "0",
            pPassword: $("#txt_LoginPassword").val() || "0",
            pIsRemember: $("#chk_LoginRemember").is(":checked")
        };
        var overlay = new COverlay({
            autoShow: false
        });
        overlay.Init();
        $.Ajax({
            url: "../Accounts/Service/AccountService.asmx/Login",
            type: 'POST',
            data: pData,
            success: function (data, textStatus, xhr) {
                if (data) {
                    data = $.findInXml(data);
                    if (data) {
                        data = eval("(" + data + ")");
                        var IsSuccessfull = data.IsSuccessfull;
                        var Description = data.Description;
                        if (IsSuccessfull) {
                            var LastName = data.Member.LastName;
                            var Avatar = data.Member.Avatar;
                            _SYS_GLOBAL.MEMBER.ID = data.Member.ID;
                            $(".main-user-logged-Name").text(LastName);
                            $(".main-user").hide();
                            $(".main-user").hide();
                            $(".main-user-logged").show();
                            $("#imgLoggedAvatar").attr("src", Avatar);
                            thisObj.Hide();
                            location.reload();
                        }
                        else {
                            $("#lbl_LoginMessages").text(Description).show();
                        }
                    }
                    else {

                    }
                }
            },
            beforeSend: function () {
                overlay.Show();
            },
            complete: function () {
                overlay.Hide();
            },
            error: function (xhr, textStatus, errorThrown) {
                overlay.Hide();
            }
        });
    },
    Event: function () {
        var thisObj = this;
        $("#btn_LoginSubmit").click(function () {
            thisObj.Submit();
        });
        $("#txt_LoginPassword").bind('keydown', 'Return', function (evt) {
            thisObj.Submit();
            return false;
        });
    }
}
$(function () {
    Axis.View.Member.Login.Init();
});

function ShowDialogLogin() {
    Axis.View.Member.Login.Toggle();
}